#include "console_output_strategy.h"

#include <cstring>
#include <string>
#include <sys/uio.h>
#include <unistd.h>

namespace Base {
namespace SystemLog {
ConsoleOutputStrategy::ConsoleOutputStrategy(bool useStderr)
    : fd_(useStderr ? STDERR_FILENO : STDOUT_FILENO)
{}

ErrCode ConsoleOutputStrategy::Write(const char* data, size_t len)
{
    std::lock_guard<std::mutex> lock(mutex_);
    ssize_t written = write(fd_, data, len);
    return written == static_cast<ssize_t>(len) ? ERR_OK : ERR_IO_WRITE_FAILED;
}

ErrCode ConsoleOutputStrategy::Flush()
{
    // 控制台不需要显式刷新
    return ERR_OK;
}

ErrCode ConsoleOutputStrategy::Close()
{
    // 控制台不需要关闭
    return ERR_OK;
}

std::string ConsoleOutputStrategy::GetName() const
{
    return "ConsoleOutput";
}
} // namespace SystemLog
} // namespace Base
